/**
 * 
 */
package edu.umd.clip.lm.model;

import org.w3c.dom.*;

import edu.umd.clip.lm.questions.*;
import edu.umd.clip.lm.util.*;
/**
 * @author Denis Filimonov <den@cs.umd.edu>
 *
 */
public class HistoryTreePayload extends XMLizable {
	private static final long serialVersionUID = 1L;
	
	public static final String XML_ELEMENT_NAME = "history-tree-payload";
	public static final String XML_CLUSTERID_ATTR_NAME = "cluster-id";
	public static final String XML_LAMBDA_ATTR_NAME = "lambda";
	public static final String XML_TOP_ATTR_NAME = "is-top";
	public static final String XML_BACKOFF_ATTR_NAME = "bo";
	public static final String XML_IS_BACKOFF_ATTR_NAME = "is-backoff";
	
	public Question question;
	public int clusterid = -1; 
	public double lambda = 1.0;
	//public double backoff = 0.0;
	public boolean top = false; // whether this is the top node for in-tree smoothing purpose
	public boolean isBackoff = false;
	
	/**
	 * @param trainData
	 * @param devData
	 * @param question
	 */
	public HistoryTreePayload(Question question) {
		this.question = question;
	}
	/*
	public boolean isLeaf() {
		return question == null;
	}
	*/
	/**
	 * @param xmlnode
	 */
	public HistoryTreePayload(Element xmlnode) {
		assert(xmlnode.getTagName().equals(XML_ELEMENT_NAME));
		String clusterAttr = xmlnode.getAttribute(XML_CLUSTERID_ATTR_NAME);
		if (!clusterAttr.equals("")) {
			clusterid = Integer.parseInt(clusterAttr);
		}
		
		String lambdaAttr = xmlnode.getAttribute(XML_LAMBDA_ATTR_NAME);
		if (!lambdaAttr.equals("")) {
			lambda = Double.parseDouble(lambdaAttr);
		}
		
		String topAttr = xmlnode.getAttribute(XML_TOP_ATTR_NAME);
		if (!topAttr.equals("")) {
			top = Boolean.parseBoolean(topAttr);
		}
		
		/*
		String boAttr = xmlnode.getAttribute(XML_BACKOFF_ATTR_NAME);
		if (!boAttr.isEmpty()) {
			backoff = Double.parseDouble(boAttr);
		}
		*/
		
		String isBackoffAttr = xmlnode.getAttribute(XML_IS_BACKOFF_ATTR_NAME);
		if (!isBackoffAttr.isEmpty()) {
			isBackoff = Boolean.parseBoolean(isBackoffAttr);
		}
		
		Element questionElement = XMLUtils.getFirstElementByTagName(xmlnode, Question.XML_ELEMENT_NAME);
		if (questionElement != null) {
			question = Question.fromXML(questionElement);
		}
		//assert(clusterid == -1 || question == null);
	}

	/* (non-Javadoc)
	 * @see edu.umd.clip.lm.util.XMLizable#createXML(org.w3c.dom.Document)
	 */
	@Override
	public Element createXML(Document doc) {
		Element element = doc.createElement(XML_ELEMENT_NAME);
		if (question != null) {
			element.appendChild(question.createXML(doc));
		}
		if (clusterid != -1) {
			element.setAttribute(XML_CLUSTERID_ATTR_NAME, Integer.toString(clusterid));
		}
		if (lambda != 1.0) {
			element.setAttribute(XML_LAMBDA_ATTR_NAME, Double.toString(lambda));
		}
		if (top) {
			element.setAttribute(XML_TOP_ATTR_NAME, Boolean.toString(top));
		}
		/*
		if (backoff != 0.0) {
			element.setAttribute(XML_BACKOFF_ATTR_NAME, Double.toString(backoff));
		}
		*/
		if (isBackoff) {
			element.setAttribute(XML_IS_BACKOFF_ATTR_NAME, Boolean.toString(isBackoff));
		}
		return element;
	}
	/* (non-Javadoc)
	 * @see edu.umd.clip.lm.util.XMLizable#getElementName()
	 */
	@Override
	public String getElementName() {
		return XML_ELEMENT_NAME;
	}
	
}
